
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>React-Redux · React 基础学习资料</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="小翼">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="03.html" />
    
    
    <link rel="prev" href="01.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    React 手稿
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter01/00.html">
            
                <a href="../chapter01/00.html">
            
                    
                    基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter01/01.html">
            
                <a href="../chapter01/01.html">
            
                    
                    初识React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter01/02.html">
            
                <a href="../chapter01/02.html">
            
                    
                    React 特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter01/03.html">
            
                <a href="../chapter01/03.html">
            
                    
                    React Initial Project
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter01/04.html">
            
                <a href="../chapter01/04.html">
            
                    
                    Component和Dom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../chapter01/05.html">
            
                <a href="../chapter01/05.html">
            
                    
                    生态组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter02/00.html">
            
                <a href="../chapter02/00.html">
            
                    
                    组件生命周期
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter02/01.html">
            
                <a href="../chapter02/01.html">
            
                    
                    装载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter02/02.html">
            
                <a href="../chapter02/02.html">
            
                    
                    更新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter02/03.html">
            
                <a href="../chapter02/03.html">
            
                    
                    卸载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter02/04.html">
            
                <a href="../chapter02/04.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter02/05.html">
            
                <a href="../chapter02/05.html">
            
                    
                    React.Component
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="00.html">
            
                <a href="00.html">
            
                    
                    state
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="01.html">
            
                <a href="01.html">
            
                    
                    Component state
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2" data-path="02.html">
            
                <a href="02.html">
            
                    
                    React-Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="03.html">
            
                <a href="03.html">
            
                    
                    React-Router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="04.html">
            
                <a href="04.html">
            
                    
                    redux-saga
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter04/00.html">
            
                <a href="../chapter04/00.html">
            
                    
                    组件其他方法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter04/01.html">
            
                <a href="../chapter04/01.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter04/02.html">
            
                <a href="../chapter04/02.html">
            
                    
                    类属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter04/03.html">
            
                <a href="../chapter04/03.html">
            
                    
                    实例属性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter05/00.html">
            
                <a href="../chapter05/00.html">
            
                    
                    React 高级用法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter05/01.html">
            
                <a href="../chapter05/01.html">
            
                    
                    keys
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter05/02.html">
            
                <a href="../chapter05/02.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter05/03.html">
            
                <a href="../chapter05/03.html">
            
                    
                    Refs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../chapter05/04.html">
            
                <a href="../chapter05/04.html">
            
                    
                    Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../chapter05/05.html">
            
                <a href="../chapter05/05.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../chapter05/06.html">
            
                <a href="../chapter05/06.html">
            
                    
                    高阶组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../chapter05/07.html">
            
                <a href="../chapter05/07.html">
            
                    
                    类型查检
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../chapter05/08.html">
            
                <a href="../chapter05/08.html">
            
                    
                    代码分隔
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter06/00.html">
            
                <a href="../chapter06/00.html">
            
                    
                    React 实例
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter06/01.html">
            
                <a href="../chapter06/01.html">
            
                    
                    List
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter06/02.html">
            
                <a href="../chapter06/02.html">
            
                    
                    Form
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter06/03.html">
            
                <a href="../chapter06/03.html">
            
                    
                    Form高级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter06/04.html">
            
                <a href="../chapter06/04.html">
            
                    
                    Tooltip
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter07/00.html">
            
                <a href="../chapter07/00.html">
            
                    
                    React项目打包部署
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter07/01.html">
            
                <a href="../chapter07/01.html">
            
                    
                    与服务端交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter07/02.html">
            
                <a href="../chapter07/02.html">
            
                    
                    打包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter07/03.html">
            
                <a href="../chapter07/03.html">
            
                    
                    部署
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter08/00.html">
            
                <a href="../chapter08/00.html">
            
                    
                    浅谈ES
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter08/01.html">
            
                <a href="../chapter08/01.html">
            
                    
                    ES6 (ES2015)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter08/02.html">
            
                <a href="../chapter08/02.html">
            
                    
                    ES7 (ES2016)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter08/03.html">
            
                <a href="../chapter08/03.html">
            
                    
                    ES8 (ES2017)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter08/04.html">
            
                <a href="../chapter08/04.html">
            
                    
                    ES9 (ES2018)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter09/00.html">
            
                <a href="../chapter09/00.html">
            
                    
                    React Hooks
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter09/01.html">
            
                <a href="../chapter09/01.html">
            
                    
                    State Hooks useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter09/02.html">
            
                <a href="../chapter09/02.html">
            
                    
                    State Hooks useReducer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >React-Redux</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="react-redux">React-Redux</h1>
<h2 id="redux">Redux</h2>
<ul>
<li><p>Action</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addTodo</span>(<span class="hljs-params">text</span>) </span>{
  <span class="hljs-keyword">return</span> {
    type: ADD_TODO,
    text
  }
}
</code></pre>
<p><code>type</code> &#x5C5E;&#x6027;&#x662F;&#x5FC5;&#x987B;&#x7684;&#x3002;</p>
</li>
<li><p>Reducer</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">todoApp</span>(<span class="hljs-params">state = initialState, action</span>) </span>{
  <span class="hljs-keyword">switch</span> (action.type) {
    <span class="hljs-keyword">case</span> ADD_TODO:
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">Object</span>.assign({}, state, {
        todo: action.todo
      })
    <span class="hljs-keyword">default</span>:
      <span class="hljs-keyword">return</span> state
  }
}
</code></pre>
<p>1&#x3001;&#x9700;&#x8981;&#x4E00;&#x4E2A;<code>case</code>&#x4E0E;<code>action</code>&#x7684;<code>type</code>&#x4E00;&#x81F4;&#x3002;</p>
<p>2&#x3001;&#x5FC5;&#x987B;&#x5728;<code>switch</code>&#x7684;<code>defautl</code>&#x8FD4;&#x56DE;&#x539F;<code>state</code>&#x3002;</p>
</li>
<li><p>Store</p>
<ul>
<li><p>&#x7BA1;&#x7406;&#x6574;&#x4E2A;&#x5E94;&#x7528;&#x7684; <code>state</code></p>
</li>
<li><p>&#x63D0;&#x4F9B;<code>getState()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;<code>state</code></p>
</li>
<li><p>&#x901A;&#x8FC7;<code>dispatch(action)</code>&#x8BBE;&#x7F6E;<code>state</code></p>
</li>
<li><p>&#x63D0;&#x4F9B;<code>subscribe(listener)</code>&#x6CE8;&#x518C;&#x548C;&#x53D6;&#x6D88;&#x6CE8;&#x518C;&#x76D1;&#x542C;&#x4E8B;&#x4EF6;</p>
</li>
</ul>
</li>
</ul>
<h2 id="react-redux">React-Redux</h2>
<blockquote>
<p>&#x662F;Redux&#x7684;React&#x5B9E;&#x73B0;,&#x53EF;&#x53C2;&#x8003; <a href="https://redux.js.org/basics/usagewithreact" target="_blank">Official React bindings for Redux</a>.</p>
</blockquote>
<h3 id="&#x5B89;&#x88C5;">&#x5B89;&#x88C5;</h3>
<pre><code>  yarn add redux react-redux

  //or

  npm i redux react-redxu
</code></pre><h3 id="&#x7EC4;&#x4EF6;&#x5C42;&#x7EA7;&#x5212;&#x5206;">&#x7EC4;&#x4EF6;&#x5C42;&#x7EA7;&#x5212;&#x5206;</h3>
<ul>
<li><p>&#x8868;&#x73B0;&#x6027;&#x7EC4;&#x4EF6;</p>
<p>&#x53EA;&#x8D1F;&#x8D23;&#x663E;&#x793A;UI&#x7684;&#x7EC4;&#x4EF6;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">&lt;div&gt;<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
</code></pre>
</li>
<li><p>&#x5BB9;&#x5668;&#x6027;&#x7EC4;&#x4EF6;</p>
<p>&#x4E0D;&#x5904;&#x7406;UI&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x4E5F;&#x4E0D;&#x5904;&#x7406;&#x6570;&#x636E;&#xFF0C;&#x53EA;&#x4E3A;UI&#x7EC4;&#x4EF6;&#x63D0;&#x4F9B;&#x6570;&#x636E;&#x7684;&#x7EC4;&#x4EF6;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx">connect(state =&gt; state)(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">YourComponent</span> /&gt;</span>)
</span></code></pre>
</li>
<li><p>&#x5176;&#x4ED6;&#x7EC4;&#x4EF6;</p>
<p>&#x9664;&#x4EE5;&#x4E0A;&#x4E24;&#x79CD;&#x7EC4;&#x4EF6;&#x4EE5;&#x5916;&#x7684;&#x7EC4;&#x4EF6;&#x3002;</p>
</li>
</ul>
<h3 id="&#x4F7F;&#x7528;redux">&#x4F7F;&#x7528;Redux</h3>
<p>&#x6211;&#x4EEC;&#x4EE5;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x4F8B;&#x5B50;&#x6765;&#x8BF4;&#x660E;&#x5982;&#x4F55;&#x4F7F;&#x7528;React-redux.</p>
<ul>
<li><p>&#x8BBE;&#x8BA1;&#x8868;&#x73B0;&#x6027;&#x7EC4;&#x4EF6;</p>
<p>&#x9700;&#x8981;&#xFF1A;&#x663E;&#x793A;&#x5F53;&#x524D;&#x8BA1;&#x6570;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;0&#xFF1B;&#x7D2F;&#x52A0;&#x6309;&#x94AE;&#xFF0C;&#x6BCF;&#x6B21;&#x5355;&#x51FB;&#x52A0;1&#xFF1B;&#x9012;&#x51CF;&#x6309;&#x94AE;&#xFF0C;&#x6BCF;&#x6B21;&#x5355;&#x51FB;&#x51CF;1&#xFF1B;&#x91CD;&#x7F6E;&#x6309;&#x94AE;&#xFF0C;&#x5355;&#x51FB;&#x5C06;&#x8BA1;&#x6570;&#x8BBE;&#x7F6E;&#x4E3A;0&#x3002;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ({counter = <span class="hljs-number">0</span>}) =&gt; {

  <span class="hljs-keyword">const</span> styles = {
    display: <span class="hljs-string">&apos;flex&apos;</span>,
    justifyContent: <span class="hljs-string">&apos;space-around&apos;</span>
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>&#x5F53;&#x524D;&#x8BA1;&#x6570;&#x4E3A;&#xFF1A;{counter}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>&#x52A0;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>&#x51CF;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>&#x91CD;&#x7F6E;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
</code></pre>
</li>
<li><p>&#x8BBE;&#x7F6E;&#x5BB9;&#x5668;&#x6027;&#x7EC4;&#x4EF6;</p>
<p>&#x76EE;&#x7684;&#x662F;&#x5C06;&#x8868;&#x73B0;&#x6027;&#x7EC4;&#x4EF6;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x62BD;&#x79BB;&#x51FA;&#x53BB;&#x4EA4;&#x7ED9;redux&#x7BA1;&#x7406;&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7;&#x5BB9;&#x5668;&#x6027;&#x7EC4;&#x4EF6;&#x5C06;redxu&#x4E0E;&#x8868;&#x73B0;&#x6027;&#x7EC4;&#x4EF6;&#x5173;&#x8054;&#x8D77;&#x6765;&#x3002;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> CounterIndex <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../components/chapter03/Counter&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span>
  connect(state =&gt; ({ ...state.counterRedux }))(({ counter, dispatch }) =&gt; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">CounterIndex</span> <span class="hljs-attr">counter</span>=<span class="hljs-string">{counter}</span> <span class="hljs-attr">dispatch</span>=<span class="hljs-string">{dispatch}</span> /&gt;</span>);
</span></code></pre>
<ul>
<li>Action</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> COUNTER_ACTION = <span class="hljs-string">&apos;COUNTER_ACTION&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> addActioner = ({counter}) =&gt; {
  <span class="hljs-keyword">return</span> {type: COUNTER_ACTION, counter: counter + <span class="hljs-number">1</span>};
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> subActioner = ({counter}) =&gt; {
  <span class="hljs-keyword">return</span> {type: COUNTER_ACTION, counter: counter - <span class="hljs-number">1</span>};
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> resetActioner = () =&gt; {
  <span class="hljs-keyword">return</span> {type: COUNTER_ACTION, counter: <span class="hljs-number">0</span>};
};
</code></pre>
<ul>
<li>Reducer</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { COUNTER_ACTION } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../actions/CounterAction&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> (state, action) =&gt; {
  <span class="hljs-keyword">switch</span> (action.type) {
    <span class="hljs-keyword">case</span> COUNTER_ACTION:
      <span class="hljs-keyword">return</span> { ...state, counter: action.counter };
    <span class="hljs-keyword">default</span>:
      <span class="hljs-keyword">return</span> state;
  }
}
</code></pre>
<ul>
<li>Store </li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> { Provider } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> { createStore } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> CounterReducer <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../reducers/CounterReducer&apos;</span>;

<span class="hljs-keyword">let</span> store = createStore(CounterReducer);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ({ children }) =&gt; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>{children}<span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
</code></pre>
</li>
</ul>
<h3 id="redux-&#x8C03;&#x8BD5;&#x63D2;&#x4EF6;">Redux &#x8C03;&#x8BD5;&#x63D2;&#x4EF6;</h3>
<ul>
<li><p>console&#x65E5;&#x5FD7;</p>
<p>&#x5B89;&#x88C5;redux-logger&#x7EC4;&#x4EF6;&#xFF1A;<code>yarn add redux-logger --dev</code>&#x3002;</p>
<p>&#x7136;&#x540E;&#x52A0;&#x5165;&#x5230;redux store&#x4E2D;&#x5373;&#x53EF;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { createLogger } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-logger&apos;</span>;

store = createStore(reducers, createLogger());
</code></pre>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<p><img src="../images/redux-log.png" alt=""></p>
</li>
<li><p>redux&#x6269;&#x5C55;&#x7A0B;&#x5E8F;</p>
<p>&#x9700;&#x8981;&#x5728;Chrome&#x5E94;&#x7528;&#x5E02;&#x573A;&#x5B89;&#x88C5;<a href="https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd" target="_blank">Redux DevTools</a>.</p>
<p>&#x7136;&#x540E;&#x5728;store&#x4E2D;&#x4F7F;&#x7528;&#x589E;&#x5F3A;&#x529F;&#x80FD;&#x5C06;redux-logger&#x52A0;&#x5165;&#x5373;&#x53EF;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> logger = <span class="hljs-built_in">window</span>.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__(createLogger());
store = createStore(reducers, logger);
</code></pre>
</li>
</ul>
<p><a href="https://codesandbox.io/s/p70k2pyz20" target="_blank">&#x5728;&#x7EBF;&#x5B9E;&#x4F8B;</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="01.html" class="navigation navigation-prev " aria-label="Previous page: Component state">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="03.html" class="navigation navigation-next " aria-label="Next page: React-Router">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"React-Redux","level":"1.4.2","depth":2,"next":{"title":"React-Router","level":"1.4.3","depth":2,"path":"chapter03/03.md","ref":"./chapter03/03.md","articles":[]},"previous":{"title":"Component state","level":"1.4.1","depth":2,"path":"chapter03/01.md","ref":"./chapter03/01.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"root":"./books","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"小翼","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React 基础学习资料","language":"zh-hans","links":{"gitbook":false,"sharing":{"google":true,"facebook":true,"twitter":true}},"gitbook":"*"},"file":{"path":"chapter03/02.md","mtime":"2020-03-07T07:33:43.855Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-09T08:08:41.080Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

